package com.xbstar.datafusion.front;

import com.alibaba.fastjson.JSONObject;
import com.xbstar.datafusion.util.ObjectManage;
import com.xbstar.datafusion.databus.DataBusManage;
import com.xbstar.datafusion.interfaces.user.UserManange;
import com.xbstar.datafusion.store.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by jiwenfei on 2018/1/30 15:16 .
 */
@RestController
@RequestMapping("/http")
public class HttpFrontResoler {
    @Autowired
    UserManange userManange;
    @Autowired
    StoreService storeService;
    @Autowired
    ObjectManage objectManage;
    @Autowired
    DataBusManage dataBusManage;
    @RequestMapping("/resoler")
    public String resolver( HttpServletRequest request, HttpServletResponse response){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String userKey= request.getParameter("userkey");
        String systemId=userManange.getSystemIdByKey(userKey);
        String time=request.getParameter("time");
        if(time==null)time=System.currentTimeMillis()/1000+"";
        String data=request.getParameter("data");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id",systemId);
        jsonObject.put("time",time);
        jsonObject.put("data",data);
        jsonObject.put("msgId", systemId+time);
        storeService.originalStore(jsonObject.toString());
        objectManage.updateObjectBySendMsg(systemId, jsonObject);
        dataBusManage.send2Controller(data);
        return  "";
    }
    @RequestMapping("/login")
    public String login(String systemId,String password){
        return userManange.login(systemId,password);
    }
    @RequestMapping("/echo")
    public String echo(String systemId){
        //心跳
        /**
         * 1. 更新物状态
         * 2.查询有没有未接受信息
         */
        return null;
    }

}
